{***************************************************************
    SpyGestion  Copyright (C) 2008 Christophe KOALI <nature2gas@gmail.com>
    
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
    
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
    
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
    
****************************************************************}

(*******************************************************************************
 Le but est d'avoir qu'une seule et unique classe qui s'occupe du parsing

 Sur cette classe, on greffe sur le vol le parsing de contenu, on se retrouve
 ainsi comme dans l'ancien système, avec une unique classe pour le menu et
 des classes différentes pour chaque page, par contre, on gagne avec l'header
 qui devient commun (et peut-être optionnel pour la page Galaxie)

*******************************************************************************)
unit modParsingClassGestion;

interface

uses modParsingClassBase,       // gestion de la répartition des parsings
     modParsingClassIHeader,    // parsing header
     modParsingClassIMenuLeft,  // parsing menu left

     modParsingClassIElements,  // éléments
     modParsingClassIRessource, // ressources

     modParsingClassIFlotte1,   // flotte 1
     modParsingClassIFlotte2,   // flotte 2
     modParsingClassIFlotte3,   // flotte 3
     modParsingClassIFlotte4,   // flotte 4

     modParsingClassIApercu,    // Aperçu
     modParsingClassIGalaxie,   // galaxie
     modParsingClassIStatistiques, // stats

     modParsingClassIMessage,   // messages

     sFonctionsChaines,
     LibXmlComps,
     Windows;


type
   TParsingBufferFonction = function (const Buffer: string): boolean of object;

   TParsingGestion = class(TParsingBase)
   private
      // Protective Critical Section object
      // Existe déjà dans: TParsingBase
      //ReadWriteCS: TRTLCriticalSection;

      // Header
      FParsingHeader: TParsingHeader;

      // Menu left
      FParsingMenuLeft: TParsingMenuLeft;




      
      // Elements
      FParsingElements: TParsingElementsGID;

      // Ressource
      FParsingRessources: TParsingRessource;


      // Flotte
      FParsingFlotte1: TParsingFlotte1_ChoixVaisseaux;
      FParsingFlotte2: TParsingFlotte2_ChoixDestination;
      FParsingFlotte3: TParsingFlotte3_OrdreEtRessources;
      FParsingFlotte4: TParsingFlotte4_ConfirmationEnvoi;

      // Apercu
      FParsingApercu : TParsingApercu;

      // Galaxie
      FParsingGalaxie: TParsingGalaxie;

      // Stats
      FParsingStats: TParsingPlayerStats;

      // Messages
      FParsingMessages: TParsingMessagesList;


      // Derniere page parsée
      FParsingLastPage: IParsingRedirection;
      FLastPageParsingFonction: TParsingBufferFonction;

      function GetLastPageURL: string;


      procedure DoJobBefore;
      procedure DoJobAfter(Resultat: boolean);

   protected
      procedure SetDebugProc(Value: TsfcProcStatus); override;

   public
      property LastPage: IParsingRedirection  read FParsingLastPage;
      property LastPageURL    : string        read GetLastPageURL;
      property LastPageParsingMethod: TParsingBufferFonction read FLastPageParsingFonction;

      // prévision au cas où le menu changerai à nouveau, contrairement à LastPageURL
      // on sera certain que LastPageURLMenu correspond à une page qui contient le menu
      property LastPageURLMenu: string        read GetLastPageURL;


      //
      // Rend accessible les classes
      //

      // Les deux principales
      property pHeader  : TParsingHeader   read FParsingHeader;
      property pMenuLeft: TParsingMenuLeft read FParsingMenuLeft;


      // Les autres

      property pElements: TParsingElementsGID read FParsingElements;
      property pRessources: TParsingRessource read FParsingRessources;

      property pFlotte1: TParsingFlotte1_ChoixVaisseaux    read FParsingFlotte1;
      property pFlotte2: TParsingFlotte2_ChoixDestination  read FParsingFlotte2;
      property pFlotte3: TParsingFlotte3_OrdreEtRessources read FParsingFlotte3;
      property pFlotte4: TParsingFlotte4_ConfirmationEnvoi read FParsingFlotte4;

      property pApercu  : TParsingApercu  read FParsingApercu;
      property pGalaxie : TParsingGalaxie read FParsingGalaxie;
      property pStats   : TParsingPlayerStats read FParsingStats;

      property pMessages : TParsingMessagesList read FParsingMessages;


      

      //
      // Fonctions de parsing
      //

      function ParsingApercu(const Buffer: string): boolean;
      function ParsingGalaxie(const Buffer: string): boolean;
      function ParsingStats(const Buffer: string): boolean;

      function ParsingMessages(const Buffer: string): boolean;


      function ParsingElements(const Buffer: string): boolean;
      function ParsingRessources(const Buffer: string): boolean;
      
      function ParsingFlotte1(const Buffer: string): boolean;
      function ParsingFlotte2(const Buffer: string): boolean;
      function ParsingFlotte3(const Buffer: string): boolean;
      function ParsingFlotte4(const Buffer: string): boolean;

      procedure SetLastPageFlotte1;


      procedure Initialisation;

      constructor Create(Scanner: TEasyXmlScanner);
      destructor Destroy; override;
   end;



implementation

procedure TParsingGestion.SetLastPageFlotte1;
begin
   // on ne peut pas changer de planète à partir de la page 4
   // on définit donc le parsing comme la page 1
   // Il faut donc faire en sorte dans le gestionnaire que le lien de la page 1
   // soit sélectionné lors du changement de planète
   FParsingLastPage := FParsingFlotte1;
   FLastPageParsingFonction := ParsingFlotte1;

end;


procedure TParsingGestion.Initialisation;
begin
    FParsingLastPage := nil;
    FLastPageParsingFonction := nil;
end;

function TParsingGestion.GetLastPageURL: string;
begin
    if FParsingLastPage <> nil then begin
       Result := FParsingLastPage.URLOfThisPage;
    end else begin
       // bizarre
       Result := URLOfThisPage;
    end;
end;


procedure TParsingGestion.SetDebugProc(Value: TsfcProcStatus);
begin
    inherited;
    
    {$IFDEF DEBUG_PARSING}
       if FParsingHeader <> nil then begin
          FParsingHeader.DebugProc := DoDebug;
       end;
       if FParsingMenuLeft <> nil then begin
          FParsingMenuLeft.DebugProc := DoDebug;
       end;
       
       if FParsingElements <> nil then begin
          FParsingElements.DebugProc := DoDebug;
       end;
       if FParsingRessources <> nil then begin
          FParsingRessources.DebugProc := DoDebug;
       end;

       if FParsingFlotte1 <> nil then begin
          FParsingFlotte1.DebugProc := DoDebug;
       end;
       if FParsingFlotte2 <> nil then begin
          FParsingFlotte2.DebugProc := DoDebug;
       end;
       if FParsingFlotte3 <> nil then begin
          FParsingFlotte3.DebugProc := DoDebug;
       end;
       if FParsingFlotte4 <> nil then begin
          FParsingFlotte4.DebugProc := DoDebug;
       end;


       if FParsingApercu <> nil then begin
          FParsingApercu.DebugProc := DoDebug;
       end;
       if FParsingGalaxie <> nil then begin
          FParsingGalaxie.DebugProc := DoDebug;
       end;
       if FParsingStats <> nil then begin
          FParsingStats.DebugProc := DoDebug;
       end;

       if FParsingMessages <> nil then begin
          FParsingMessages.DebugProc := DoDebug;
       end;



    {$ENDIF}
    
end;

procedure TParsingGestion.DoJobBefore;
begin
   Lock;
   FParsingLastPage         := IContent;

   FParsingMenuLeft.SaveToBackup;
end;


procedure TParsingGestion.DoJobAfter(Resultat: boolean);
begin
   if Resultat = false then begin
      // restaure les liens du menu
      FParsingMenuLeft.RestaureBackup;
   end;
   
   Unlock;
end;


function TParsingGestion.ParsingMessages(const Buffer: string): boolean;
begin
   IContent := FParsingMessages;
   DoJobBefore;

   FLastPageParsingFonction := ParsingMessages;

   
   Result   := ParseBuffer(Buffer);
   DoJobAfter(Result);
end;

function TParsingGestion.ParsingStats(const Buffer: string): boolean;
begin
   IContent := FParsingStats;
   DoJobBefore;

   FLastPageParsingFonction := ParsingStats;


   Result   := ParseBuffer(Buffer);
   DoJobAfter(Result);
end;


function TParsingGestion.ParsingGalaxie(const Buffer: string): boolean;
begin
   IContent := FParsingGalaxie;
   DoJobBefore;

   // pour éviter l'initialisation de l'header et donc la perte des planètes
   IHeader  := nil;

   FLastPageParsingFonction := ParsingGalaxie;

   Result   := ParseBuffer(Buffer);

   // restaure l'header
   IHeader  := FParsingHeader;
   
   DoJobAfter(Result);
end;

function TParsingGestion.ParsingApercu(const Buffer: string): boolean;
begin
   IContent := FParsingApercu;
   DoJobBefore;

   FLastPageParsingFonction := ParsingApercu;

   Result   := ParseBuffer(Buffer);
   DoJobAfter(Result);
end;



function TParsingGestion.ParsingFlotte4(const Buffer: string): boolean;
begin
   IContent := FParsingFlotte4;
   DoJobBefore;

   FLastPageParsingFonction := ParsingFlotte4;

   Result   := ParseBuffer(Buffer);

   DoJobAfter(Result);
end;

function TParsingGestion.ParsingFlotte3(const Buffer: string): boolean;
begin
   IContent := FParsingFlotte3;
   DoJobBefore;

   FLastPageParsingFonction := ParsingFlotte3;

   Result   := ParseBuffer(Buffer);
   DoJobAfter(Result);
end;

function TParsingGestion.ParsingFlotte2(const Buffer: string): boolean;
begin
   IContent := FParsingFlotte2;
   DoJobBefore;

   FLastPageParsingFonction := ParsingFlotte2;

   Result   := ParseBuffer(Buffer);
   DoJobAfter(Result);
end;

function TParsingGestion.ParsingFlotte1(const Buffer: string): boolean;
begin
   IContent := FParsingFlotte1;
   DoJobBefore;

   FLastPageParsingFonction := ParsingFlotte1;

   Result   := ParseBuffer(Buffer);
   DoJobAfter(Result);
end;

function TParsingGestion.ParsingRessources(const Buffer: string): boolean;
begin
   IContent := FParsingRessources;
   DoJobBefore;

   FLastPageParsingFonction := ParsingRessources;

   Result   := ParseBuffer(Buffer);
   DoJobAfter(Result);
end;

function TParsingGestion.ParsingElements(const Buffer: string): boolean;
begin
   IContent := FParsingElements;
   DoJobBefore;

   FLastPageParsingFonction := ParsingElements;

   Result   := ParseBuffer(Buffer);
   DoJobAfter(Result);
end;

constructor TParsingGestion.Create(Scanner: TEasyXmlScanner);
begin
    inherited;

    FParsingHeader     := TParsingHeader.Create;
    FParsingMenuLeft   := TParsingMenuLeft.Create;

    FParsingElements   := TParsingElementsGID.Create;
    FParsingRessources := TParsingRessource.Create;

    FParsingFlotte1   := TParsingFlotte1_ChoixVaisseaux.Create;
    FParsingFlotte2   := TParsingFlotte2_ChoixDestination.Create;
    FParsingFlotte3   := TParsingFlotte3_OrdreEtRessources.Create;
    FParsingFlotte4   := TParsingFlotte4_ConfirmationEnvoi.Create;

    FParsingApercu    := TParsingApercu.Create;
    FParsingGalaxie   := TParsingGalaxie.Create;
    FParsingStats     := TParsingPlayerStats.Create;

    FParsingMessages  := TParsingMessagesList.Create;

    Self.AdvDivTableGestion := true;

    IHeader   := FParsingHeader;
    IMenuLeft := FParsingMenuLeft;


    FParsingLastPage := nil;

    Initialisation;
end;

destructor TParsingGestion.Destroy;
begin
    FParsingHeader.Free;
    FParsingMenuLeft.Free;

    FParsingElements.Free;
    FParsingRessources.Free;

    FParsingFlotte1.Free;
    FParsingFlotte2.Free;
    FParsingFlotte3.Free;
    FParsingFlotte4.Free;

    FParsingApercu.Free;
    FParsingGalaxie.Free;
    FParsingStats.Free;

    FParsingMessages.Free;

    inherited;
end;



end.
